//
// Created by HP on 2025/11/12.
//

#ifndef FLIGHTSERVER_REDIS_H
#define FLIGHTSERVER_REDIS_H

#include <string>
#include <iostream>
#include <hiredis/hiredis.h>

class RdConnector {
    std::string host_;
    std::string port_;
    std::string password_;
    int db;
    redisContext* context;

    static RdConnector* instance_;
public:
    RdConnector(std::string host, std::string port, std::string password, int db) : host_(std::move(host)), port_(std::move(port)), password_(std::move(password)), db(db), context(
            nullptr) {
    }

    ~RdConnector() {
        if(context != nullptr){
            redisFree(context);
            context = nullptr;
            std::cout<<"redis context free"<<std::endl;
        }
    }

    bool connect() ;

    std::string get(const std::string& key) ;

    bool set(const std::string& key, const std::string& value) ;

    bool set(const std::string& key, const std::string& value, int expireSeconds) ;

    bool exists(const std::string& key) ;

    static RdConnector* getInstance();

    std::string getError();
};

#endif //FLIGHTSERVER_REDIS_H
